﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
		<title>Dapper Usage</title>
		<link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
	</head>

	<body>

		<div class="document-contents">


		<h3 id="DocIntroduction">Introduction</h3>
			<p><a href="https://github.com/StackExchange/Dapper" target="_blank">Dapper</a> 
			is an object-relational mapper (ORM) for .NET. 
			<a href="https://www.nuget.org/packages/Abp.Dapper" target="_blank">Abp.Dapper</a> package simply integrates Dapper to ASP.NET Boilerplate. 
			It works as secondary ORM provider with EF 6.x, EF Core or 
			NHibernate.</p>

			<h3 id="DocQuickStart">Installation</h3>
			<p>Before you start, you need to install <a href="https://www.nuget.org/packages/Abp.Dapper" target="_blank">Abp.Dapper</a> and one of EF Core, EF 6.x and NHibernate ORM nuget packages 
			to project that you want to use it.</p>
			
			<h4>Module Registration</h4>
			
			<p>First you need to add <strong>DependsOn</strong> attribute for <strong>AbpDapperModule</strong> on your module where you register it.</p>
			
			<pre lang="cs">[DependsOn(
     typeof(AbpEntityFrameworkCoreModule),
     <strong>typeof(AbpDapperModule)</strong>
)]
public class MyModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
    }
}</pre>
<p><strong>Note</strong> that AbpDapperModule dependency should be added later than EF Core 
dependency.</p>
			
			<h4>Entity to Table Mapping</h4>

			<p>You can configure mappings. For example, <strong>Person</strong> 
			class maps to <strong>Persons</strong> table in the following 
			example:</p>
			
			<pre lang="cs">public class PersonMapper : <strong>ClassMapper&lt;Person&gt;</strong>
{
    public PersonMapper()
    {
<strong>        Table("Persons");
        Map(x =&gt; x.Roles).Ignore();
        AutoMap();
</strong>    }
}</pre>
			
			<p>You should set the assemblies contains mapper classes. Excample:</p>
						<pre lang="cs">[DependsOn(
     typeof(AbpEntityFrameworkModule),
     typeof(AbpDapperModule)
)]
public class MyModule : AbpModule
{
    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
        <strong>DapperExtensions.SetMappingAssemblies(new List&lt;Assembly&gt; { typeof(MyModule).GetAssembly() });</strong>
    }
}
			</pre>
			
			<h3 id="DocUsage">Usage</h3>

			
			<p>After registering <strong>AbpDapperModule</strong>, you can use Generic IDapperRepository interface (instead of standard IRepository) to inject dapper repositories.</p>
			<pre lang="cs">public class SomeApplicationService : ITransientDependency
{
    <strong>private readonly IDapperRepository&lt;Person&gt; _personDapperRepository;</strong>
    private readonly IRepository&lt;Person&gt; _personRepository;

    public SomeApplicationService(
        IRepository&lt;Person&gt; personRepository,
        <strong>IDapperRepository&lt;Person&gt; personDapperRepository</strong>)
    {
        _personRepository = personRepository;
        _personDapperRepository = personDapperRepository;
    }

    public void DoSomeStuff()
    {
        var people = _personDapperRepository.Query("select * from Persons");
    }
}
</pre>
			<p>You can use both EF repositories and Dapper repositories at the same time, 
			in the same transaction.</p>
			

		</div>
	</body>

</html>
